<--- %%NOBANNER%% --> sv2pt_ss.sas
 BackForward

/*------------------<--- Start of Description -->--------------------\
| Sample size for comparison of proportion surviving between 2 groups|
| at a specifed follow-up time (same formula as sv2md_ss, only       |
| percent survival is used instead of median survival)               |
| Provides approximate sample size for logrank test.                 |
| Assumptions are uniform accrual and exponential survival.          |
|--------------------<--- End of Description -->---------------------|
|--------------------------------------------------------------------|
|--------------<--- Start of Files or Arguments Needed -->-----------|
| Arguments:                                                         |
|   - Required:                                                      |
|     T0 = length of accrual period(uniform accrual assumed)         |
|     T  = total length of study(=T0+follow-up after last patient)   |
|     t_y  = time at which survival curves are to be compared        |
|     y1 = true group 1 proportion surviving at time t_y             |
|     min_y2 = smallest possible true group 2 proportion surviving   |
|            at time t_y                                             |
|   - Optional:                                                      |
|     max_y2 = largest possible true proportion surviving at t_y in  |
|            group 2                                                 |
|     inc_y2 = increment value for range of y2                       |
|     r = ratio of group 2/group 1 sample size(n2/n1),default=1      |
|     alpha = type 1 error, e.g. .01 or .05, default=.05             |
|     sides = 1 or 2 for 1 or 2 sided test, default=2                |
|     power = desired power, e.g. .80, .90, default=.80              |
|     plot   = 'P' for line printer plot of group 2 sample size vs y2|
|            'G' for SAS/GRAPH plot of group 2 sample size vs y2     |
|     unit   = units for time, e.g. years, months, days, hours       |
| Output: Sample size per group for true group 1 proportion surviving|
|         =y1(at time t_y) vs true group 2 survival ranging from     |
|         min_y2 to max_y2.                                          |
|---------------<--- End of Files or Arguments Needed -->------------|
|--------------------------------------------------------------------|
|----------------<--- Start of Example and Usage -->-----------------|
| Example: %sv2pt_ss(sides=1,power=.9, y1=.40,min_y2=0.45,max_y2=.90,|
|                    inc_y2=.01,t_y=4, t0=3,t=5,unit=yrs,plot=g);    |
| Usage:   %sv2pt_ss(ALPHA=.05,SIDES=2,POWER=.80,T_Y=.,Y1=.,MIN_Y2=.,|
|                MAX_Y2=.,INC_Y2=.,R=1,T0=.,T=.,PLOT= , UNIT=);      |
| Reference: Bergstralh, EJ.  SAS macros for sample size and power   |
|             calculations.  Proceedings of the 9th annual SAS Users |
|             Group International Conference.                        |
|             Equation #17.                                          |
\-------------------<--- End of Example and Usage -->---------------*/
%MACRO sv2pt_ss(ALPHA=.05,SIDES=2,POWER=.80,T_Y=.,Y1=.,MIN_Y2=.,
                MAX_Y2=.,INC_Y2=.,R=1,T0=.,T=.,PLOT= , UNIT=);
/*--------------------------------------------\
| Author:  Michael Riggs and Eric Bergstralh; |
| Purpose: Sample size for comparison of      |
|          proportion surviving between 2     |
|          groups at a specifed follow-up time|
\--------------------------------------------*/
 OPTIONS MISSING=' ' NOCENTER;
   %LET PLOT=%UPCASE(&PLOT);
 DATA T1;
      ALPHA=&ALPHA;
      SIDES=&SIDES;
      Y1=&Y1;
      MIN_Y2=&MIN_Y2;
      MAX_Y2=&MAX_Y2;
      INC_Y2=&INC_Y2;
      POWER=&POWER;
      T_Y=&T_Y;
      R=&R;
      T0=&T0;
      T=&T;
    ZALPHA=(PROBIT(1-ALPHA))*(SIDES=1) + (PROBIT(1-ALPHA/2))*(SIDES=2);
    ZBETA=PROBIT(POWER);
    IF MAX_Y2=. THEN DO;
      MAX_Y2=MIN_Y2+1; INC_Y2=MIN_Y2+2;  *NEED 1 EXEC OF DO;
    END;
    TY1=Y1;
    TT=T;
    TT0=T0;
    TT_Y=T_Y;
    TR=R;
    DO Y2=MIN_Y2 TO MAX_Y2 BY INC_Y2;
       LAMBDA1=-LOG(TY1)/TT_Y;
       LAMBDA2=-LOG(Y2)/TT_Y;
       LAMBD_BR=(LAMBDA1+TR*LAMBDA2)/(1+TR);
       IF TT0 GT 0 THEN DO;
         PHILBDA1=LAMBDA1**2   *
                  1/ (1-(EXP(-LAMBDA1*(TT-TT0))-EXP(-LAMBDA1*TT)) /
                               (LAMBDA1*TT0) );
         PHILBDA2=LAMBDA2**2   *
                  1/ (1-(EXP(-LAMBDA2*(TT-TT0))-EXP(-LAMBDA2*TT)) /
                               (LAMBDA2*TT0) );
         PHILBDBR=LAMBD_BR**2  *
                  1/ (1-(EXP(-LAMBD_BR*(TT-TT0))-EXP(-LAMBD_BR*TT)) /
                               (LAMBD_BR*TT0) );
       END;
       IF TT0=0 THEN DO;  *ALL PTS ENTER STUDY AT SAME TIME;
          PHILBDA1=LAMBDA1**2/(1-EXP(-LAMBDA1*TT));
          PHILBDA2=LAMBDA2**2/(1-EXP(-LAMBDA2*TT));
          PHILBDBR=LAMBD_BR**2/(1-EXP(-LAMBD_BR*TT));
       END;
       N1=((ZALPHA*SQRT(PHILBDBR*(1+1/TR)) +
          ZBETA*SQRT(PHILBDA1+PHILBDA2/TR))   /
         (LAMBDA1-LAMBDA2) ) **2;
       N1=CEIL(N1);
       N2=N1*TR;
       OUTPUT;
     END;
 LABEL Y1="Group 1*Proportion*Surviving*at &t_y &unit"
       Y2="Group 2*Proportion*Surviving*at &t_y &unit"
       n1="Group 1*Sample Size"
       n2="Group 2*Sample Size"
       T_Y="Time*(&UNIT) of*Comparison";
 PROC PRINT SPLIT='*';
      ID Y1; var Y2 N1 N2;
 TITLE2
 "SAMPLE SIZE FOR COMPARING 2 SURVIVAL CURVES--INPUT IS PROP. ALIVE AT &T_y &u
nit";
 TItle3"Uniform accrual for &t0 &unit(T0) is assumed with analysis at &t &unit
 (T)";
 title4
  "Alpha=&alpha, Sides=&sides, Power=&power, Ratio N2/N1=&r";
 title5"True Group 1 Proportion alive at &t_y &unit is &y1";
%IF &MAX_Y2 NE . %THEN %DO;
    %IF &PLOT= P  %THEN %DO;
      PROC PLOT NOLEGEND;
           PLOT N2*Y2/ HAXIS=&MIN_Y2 TO &MAX_Y2 BY &INC_Y2;
            LABEL N2='Group 2 sample size'
                  Y2="True Group 2 proportion alive at &t_y &unit";
    %END;
    %ELSE %IF &PLOT= G  %THEN %DO;
          PROC GPLOT ;
           PLOT N2*Y2;
           SYMBOL1  F=SPECIAL V=J H=1 I=j       L=1;
           SYMBOL2  F=SPECIAL V=M H=1   I=j       L=2;
            LABEL N2='N2'
                  Y2="True Group 2 proportion alive at &t_y &unit";
               run; quit;
    %END;
    RUN;
%END;
%MEND sv2pt_ss;